package train48days.monimianshi;

import java.util.*;

public class 最小的K个数 {
    public ArrayList<Integer> GetLeastNumbers_Solution (int[] input, int k) {
//        ArrayList<Integer> arrayList = new ArrayList<>();
//        Arrays.sort(input);
//        for (int i = 0; i < k; i++) {
//            arrayList.add(input[i]);
//        }
//        return arrayList;
        ArrayList<Integer> arr1 = new ArrayList<>();
        if (input.length==0 || k==0){
            return arr1;
        }
        Queue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < input.length; i++) {
            if (pq.size()<k){
                pq.offer(input[i]);
            }else {
                int a = pq.peek();
                if (input[i]<a){
                    pq.poll();
                    pq.offer(input[i]);
                }
            }
        }
        int i = 0;
        while (!pq.isEmpty()){
            arr1.add(pq.poll());

        }
        return arr1;
    }
}
